package com.zhida.jiyangcenter.membership

import android.os.Bundle
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import chooongg.base.annotation.ContentLayout
import chooongg.base.utils.*
import chooongg.http.APIException
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.flyco.animation.BounceEnter.BounceTopEnter
import com.flyco.animation.SlideExit.SlideBottomExit
import com.flyco.dialog.listener.OnBtnClickL
import com.flyco.dialog.widget.NormalDialog
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.*
import com.zhida.jiyangcenter.mian.BaseFragment
import com.zhida.jiyangcenter.setting.SettingActivity
import com.zhida.jiyangcenter.utils.SP
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import com.zhida.jiyangcenter.widget.NiceImageView
import kotlinx.android.synthetic.main.fragment_buy_member.*

@ContentLayout(R.layout.fragment_buy_member)
class BuyMemberFragment : BaseFragment() {
    val adapter = AdapterBottom()
    override fun initConfig(savedInstanceState: Bundle?) {
        getData()
        bga_buy_member.setAdapter { banner, itemView, model, position ->
            if (model != null && model is MemberCenterEntity) {
                glideImage(
                    itemView.findViewById<NiceImageView>(R.id.iv_backgraoud),
                    model.card_img ?: ""
                )
                itemView.findViewById<TextView>(R.id.tv_name).text =
                    "${model.card_level}：${model.card_price}元"
            }
        }
        rv_buy_member.layoutManager = object : GridLayoutManager(context, 3, VERTICAL, false) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rv_buy_member.adapter = adapter
        tv_pay_order.setOnClickListener {
            lifecycleScope.request(
                { APIAccount.service().personalInfo() },
                object : BaseAPI<PersonalInfoEntity>() {
                    override fun onSuccess(t: PersonalInfoEntity?, state: Int, msg: String) {
                        if (t?.member?.level == "1" || t?.member?.level == "2" || t?.member?.level == "3" || t?.member?.level == "4") {
                            val dialog = NormalDialog(context)
                            dialog.content("您已购买过会员卡,是否继续购买？")
                                .style(NormalDialog.STYLE_TWO)//
                                .title("温馨提示")
                                .btnNum(2)
                                .btnTextColor(
                                    resColor(R.color.colorAccent),
                                    resColor(R.color.colorAccent)
                                )
                                .btnText("是", "否")
                                .titleTextSize(23f)
                                .showAnim(BounceTopEnter())
                                .dismissAnim(SlideBottomExit())
                                .show()
                            dialog.setOnBtnClickL(OnBtnClickL {
                                dialog.dismiss()
                                buy()
                            }, OnBtnClickL {
                                dialog.dismiss()
                            })
                        } else {
                            buy()
                        }
                    }

                    override fun onError(e: APIException) {
                        tipFail(e.eMessage)
                    }

                })
        }
        bga_buy_member.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                adapter.setNewData(datas[position].serve_list)
            }
        })
    }

    fun buy() {
        lifecycleScope.request({
            APIMemberApps.service().payCard(datas[bga_buy_member.currentItem].id ?: "")
        }, object :
            BaseAPI<PayCardEntity>() {
            override fun onSuccess(t: PayCardEntity?, state: Int, msg: String) {
                tipSuccess(msg)
                startActivity<PayMemberActivity> {
                    putExtra("code", t?.code)
                    putExtra("coupon_name", t?.coupon_name)
                    putExtra("term_of_validity", t?.term_of_validity)
                    putExtra("card_price", t?.card_price)
                }
            }

            override fun onError(e: APIException) {
                tipFail(e.eMessage)
            }

        })
    }

    lateinit var datas: ArrayList<MemberCenterEntity>
    private fun getData() {
        lifecycleScope.request({ APIMemberApps.service().memberCenter() },
            object : BaseAPI<ArrayList<MemberCenterEntity>>() {
                override fun onSuccess(t: ArrayList<MemberCenterEntity>?, state: Int, msg: String) {
                    if (t.isNullOrEmpty()) {

                    } else {
                        datas = t
                        bga_buy_member.setData(R.layout.item_buy_member, t, null)
                        adapter.setNewData(t[0].serve_list)
                    }
                }

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                }

            })
    }

    class AdapterBottom :
        BaseQuickAdapter<MemberCenterEntity.Serve, BaseViewHolder>(R.layout.item_buy_member_rv) {
        override fun convert(helper: BaseViewHolder, item: MemberCenterEntity.Serve?) {
            glideImage(helper.getView(R.id.iv_top), item?.img ?: "")
            helper.setText(R.id.tv_bottom, item?.serve_name)
        }

    }
}